package com.android.tea;

import java.util.ArrayList;
import java.util.List;

import android.app.Activity;
import android.graphics.Bitmap;
import android.graphics.Color;
import android.graphics.Bitmap.Config;
import android.graphics.drawable.BitmapDrawable;
import android.os.AsyncTask;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.ListView;
import android.widget.ProgressBar;

import com.android.tea.adapter.ListenListenAdapter;
import com.android.tea.entity.Order;

public class ListenListenActivity extends Activity implements OnClickListener {

	private Button listen_tuijian, listen_renqi;
	private ListView listView;
	private List<Order> loadData;
	private ProgressBar progressBar;
	private DataLoaderTask dataLoader;
	private ListenListenAdapter adapter;
	private boolean click;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.listen_listen);

		initComponents();
	}

	private void initComponents() {
		listen_tuijian = (Button) findViewById(R.id.listen_tuijian);
		listen_renqi = (Button) findViewById(R.id.listen_renqi);
		listen_tuijian.setOnClickListener(this);
		listen_renqi.setOnClickListener(this);

		progressBar = (ProgressBar) findViewById(R.id.progressBar1);

		listView = (ListView) findViewById(R.id.listView1);
		listView.setCacheColorHint(Color.TRANSPARENT);
		Bitmap bitmap = Bitmap.createBitmap(new int[] { 0xFFDEDEDE, 0xFFDEDEDE,
				0xFFDEDEDE, 0xFFDEDEDE, 0xFFFFFFFF, 0xFFFFFFFF, 0xFFFFFFFF,
				0xFFFFFFFF }, 2, 4, Config.RGB_565);
		listView.setDivider(new BitmapDrawable(bitmap));
		listView.setDividerHeight(2);

		loadData = loadData();
		adapter = new ListenListenAdapter(this, loadData);
		listView.setAdapter(adapter);
	}

	private List<Order> loadData() {
		List<Order> list = new ArrayList<Order>();
		Order member4 = new Order();

		list.add(member4);

		Order member = new Order();
		list.add(member);

		Order member2 = new Order();
		list.add(member2);

		Order member3 = new Order();
		list.add(member3);

		Order member5 = new Order();
		list.add(member5);

		Order member6 = new Order();
		list.add(member6);

		Order member7 = new Order();
		list.add(member7);

		Order member8 = new Order();
		list.add(member8);
		return list;
	}

	private List<Order> init() {
		List<Order> list = new ArrayList<Order>();
		Order member4 = new Order();
		member4.setOrderNumber("2344567456");
		member4.setPrice("222.00");
		member4.setCreatetime(System.currentTimeMillis());
		member4.setStatus(1);
		list.add(member4);

		Order member = new Order();
		member.setOrderNumber("467899807u");
		member.setPrice("333.00");
		member.setCreatetime(System.currentTimeMillis());
		member.setStatus(2);
		list.add(member);

		return list;
	}

	@Override
	public void onClick(View v) {
		switch (v.getId()) {
		case R.id.btn_back:
			super.onBackPressed();
			break;
		case R.id.btn_lastest_orders:
			click = false;
			if (dataLoader != null) {
				dataLoader.cancel(true);
				dataLoader = null;
			}
			dataLoader = new DataLoaderTask();
			dataLoader.execute();

			break;
		case R.id.btn_orders_before_month:
			click = true;
			if (dataLoader != null) {
				dataLoader.cancel(true);
				dataLoader = null;
			}
			dataLoader = new DataLoaderTask();
			dataLoader.execute();
			break;

		default:
			break;
		}

	}

	private final class DataLoaderTask extends AsyncTask<Void, Integer, Long> {

		@Override
		protected Long doInBackground(Void... params) {
			try {
				Thread.sleep(2000);
			} catch (InterruptedException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}

			if (click) {
				loadData = init();
			} else {
				loadData = loadData();
			}
			return null;
		}

		@Override
		protected void onPostExecute(Long result) {
			progressBar.setVisibility(View.GONE);
			listView.setVisibility(View.VISIBLE);
			adapter = new ListenListenAdapter(ListenListenActivity.this,
					loadData);
			listView.setAdapter(adapter);
			adapter.notifyDataSetChanged();
			super.onPostExecute(result);
		}

		@Override
		protected void onPreExecute() {
			listView.setVisibility(View.GONE);
			progressBar.setVisibility(View.VISIBLE);
			super.onPreExecute();
		}

	}

}
